<template>
    <div class="card">
        <!-- 搜索 -->
        <reportSearch
            @addAssert="addAssert"
            :levelType="levelType"
            :key="levelType"
            @search="search"
        ></reportSearch>
        <!-- 卡片 -->
        <ReportCardS
            v-if="levelType !== $config.card.WH.code"
            :card-data="cardData"
            @changePage="changePage"
            @jumpList="jumpList"
        ></ReportCardS>

        <!-- 线下报告维护表格 -->
        <div v-if="levelType === $config.card.WH.code" class="card-content">
            <my-table :tableData="report" @updateData="getReport"></my-table>
        </div>

        <!-- <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchFrom.pageIndex"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="searchFrom.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
        >
        </el-pagination> -->

        <!-- 上传文件 -->
        <uploadFile
            ref="uploadRef"
            @addReport="addReport"
            :uploadVisible="uploadVisible"
        ></uploadFile>
    </div>
</template>

<script>
import reportSearch from '@/components/search/reportSearch';
import ReportCardS from './modules/reportCard.vue';
import {
    getReportManageList,
    getMaintenance,
    getOfflineReport
} from '@/api/report';
import myTable from '@/components/table/cardTable';
import uploadFile from '../reportManage/modules/uploadFile.vue';

export default {
    name: 'card',
    components: {
        reportSearch,
        ReportCardS,
        myTable,
        uploadFile
    },
    data () {
        return {
            // 层级：学校层，专业层，课程层，教师层，学生层
            levelType: '',
            cardData: {},
            tableData: [],
            // 上传文件
            uploadVisible: {
                visible: false
            },
            searchFrom: {
                createTime: '',
                fileType: '',
                note: '',
                preparationDepartment: '', // 编制部门
                reportName: '', // 报告名称
                year: '',
                pageIndex: 1,
                pageSize: 10
                // currentPage: 1
            },
            report: [],
            total: 0
        };
    },
    created () {
        this.levelType = this.$route.params.type;
        this.searchFrom.reportIndexs = this.levelType;
        // this.searchFrom.reportIndex = this.levelType
        this.$store.dispatch('GET_REPORTINDEX');
        if (this.levelType === 'XXBG_WH') {
            this.getReport();
        } else {
            this.getList();
        }
        // getMaintenance(this.searchFrom).then(res => {
        //   // console.log(res)
        // })
    },
    methods: {
        // 搜索
        search (searchFrom) {
            console.log(searchFrom, 'searchFrom');
            this.searchFrom.pageIndex = 1;
            this.searchFrom.year = searchFrom.year;
            this.searchFrom.name = searchFrom.name;
            this.searchFrom.preparationDepartment =
                searchFrom.preparationDepartment;
            this.searchFrom.reportName = searchFrom.reportName;
            // this.getList()
            if (this.$route.params.type === 'XXBG_WH') {
                this.getReport();
            } else {
                this.getList();
            }
        },
        // 获取card数据
        getList () {
            getReportManageList(this.searchFrom)
                .then(res => {
                    this.cardData = res || {};
                })
                .catch(() => {});
            // getOfflineReport(this.searchFrom)
            //   .then(res => {
            //     this.cardData = res || {}
            //   })
            //   .catch(() => {})
        },
        // 跳转至详情
        jumpList (card) {
            const name = card.reportName;
            this.$router.push({
                path: `/report/reportManage/cardDetail/${this.levelType}`,
                query: {
                    id: card.id,
                    title: `${name}`,
                    standardId: card.targetStandardId
                }
            });
        },
        // 切换分页
        changePage (searchForm) {
            this.searchFrom.pageIndex = searchForm.pageIndex;
            this.searchFrom.pageSize = searchForm.pageSize;
            this.getList();
        },
        getReport () {
            getOfflineReport(this.searchFrom).then(res => {
                this.report = res.dataList;
                this.total = res.total;
            });
        },
        addAssert (reportType) {
            this.uploadVisible.visible = true;
            this.$nextTick(() => {
                this.$refs.uploadRef.closeDialog('fileForm')
            })
            this.$refs.uploadRef.reportType = reportType
            console.log(reportType, 'addAssert');
        },
        // 切换每页显示条数
        handleSizeChange (val) {
            this.searchFrom.pageSize = val;
            this.getReport();
        },
        // 分页功能
        handleCurrentChange (val) {
            this.searchFrom.pageIndex = val;
            this.getReport();
        },
        addReport () {
            this.getReport();
        }
    },
    watch: {
        $route: {
            handler (to, from) {
                this.levelType = to.params.type;
                this.searchFrom.reportIndexs = this.levelType;
                // this.searchFrom.reportIndex = this.levelType
                if (this.levelType === 'XXBG_WH') {
                    this.getReport();
                } else {
                    this.getList();
                }
            }
            // immediate: true
        }
    }
};
</script>

<style scoped>
.el-pagination {
    margin-top: 20px;
}
</style>
